package com.ohuang.wallpapermanager.vm

import android.content.Context
import android.util.Log
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.toMutableStateList
import androidx.lifecycle.ViewModel
import com.ohuang.wallpapermanager.compose.update
import com.ohuang.wallpapermanager.repository.WallPaperInfoRepository
import com.ohuang.wallpapermanager.screen.WallPaperListScreenState
import com.ohuang.wallpapermanager.screen.WallPaperListScreenStateItem
import com.ohuang.wallpapermanager.screen.toState
import com.ohuang.wallpapermanager.storage.storage.SpStorageUtil
import com.ohuang.wallpapermanager.util.AppUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class WallPaperListViewModel : ViewModel() {
    val TAG = "WallPaperListViewModel"
    private val _wallPaperListScreenState =
        mutableStateOf(WallPaperListScreenState(list = mutableStateListOf()))
    val wallPaperListScreenState: State<WallPaperListScreenState> = _wallPaperListScreenState

    private var wallPaperInfoRepository: WallPaperInfoRepository? = null
    suspend fun init(isLock: Boolean) {
        withContext(Dispatchers.IO) {

            wallPaperInfoRepository = WallPaperInfoRepository(isLock)
            val list = wallPaperInfoRepository?.getWallPaperInfoList()
                ?.map {
                    WallPaperListScreenStateItem(id = it.id, imageFile = it.filePath) }

            list?.let {
                _wallPaperListScreenState.update {
                    it.copy(list = list.toMutableStateList())
                }
            }
        }
    }


}